<?php
    include_once './config.php';
    $user = checkLogin(3);
    include_once './config.php';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $names = $_POST['names'];
        $numbers = $_POST['numbers'];
        $ids = $_POST['ids'];
        $user_id = $_POST['user_id'];
        $user_id = $user_id[0];
        if(count($names) !== count(array_unique($names))){
            $error = true;
            $msg = "名称不能出现重复";
        } else {
            //删除
           $query = "delete from whw_face where id not in (".implode(",",$ids).") and user_id=".getInt($user_id);
           $objDBCD14->execute($query);
            for($i = 0;$i < count($names);$i++){
                if($ids[$i]){
                   $query = "update whw_face set name =".quote($names[$i]).",number = ".getInt($numbers[$i])." where id=".getInt($ids[$i]);
                } else {
                   $query = "insert into whw_face (user_id,name,number,createtime) values (".$user_id.",".quote($names[$i]).",".getInt($numbers[$i]).",".time().")";
                }
                $objDBCD14->execute($query);
                $msg = "保存成功";
            }
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $faceList = [];
    $user_id = $_GET['user_id'];
    $query = "select * from whw_face where user_id=".getInt($user_id);
    $rows = $objDBCD14->query($query);
    if ($rows->num_rows) {
        foreach ($rows as $key => $face) {
            $faceList[] =  $face; 
        }  
    }
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>添加师傅</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .container{
            margin: 0;
            padding: 10px 10px 20px;
            box-sizing: border-box;
            width: 100%;
        }
        .col{
            background: #f7f7f7;
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .del{
            text-align: right;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <form class="layui-form layui-form-pane" action="" id="myForm">
          <div class="row">
            <?php if(count($faceList)) { ?>
                <?php for($i = 0;$i < count($faceList);$i++) { ?>
                    <div class="col">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="names[]" required lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input" value="<?=$faceList[$i]['name']?>"
                                <?=$user['type'] == 2 ? 'disabled' : ''?> />    
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">数量</label>
                            <div class="layui-input-block">
                                <input type="number" name="numbers[]" required lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input" value="<?=$faceList[$i]['number']?>"
                                <?=$user['type'] == 2 ? 'disabled' : ''?> />    
                            </div>
                        </div>
                        <?php if($user['type'] == 1){ ?>
                        <div class="del">
                            <input type="hidden" name="ids[]" value="<?=$faceList[$i]['id']?>"/>
                            <button type="button" class="layui-btn layui-btn-primary layui-border-black layui-btn-sm del-btn">删除</button>
                        </div>
                        <?php } ?>
                  </div>
                <?php } ?>
             <?php } ?>
            </div>
            <?php if($user['type'] == 1){ ?>
              <button type="button" class="layui-btn layui-btn-primary layui-border-black layui-btn-lg add-btn" style="width: 100%;">添加</button>
              <input type="hidden" name="user_id" value="<?=$user_id?>"/>
              <button type="submit" class="layui-btn layui-btn-danger layui-btn-lg"
              style="width: 100%;margin-top: 10px" id="submitForm">保存</button>
            <?php } ?>
        </form>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['jquery','layer', 'form','code'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code;
            // 添加
            $(".add-btn").click(function(){
                var colHtml = '<div class="col"><div class="layui-form-item"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text"name="names[]"required lay-verify="required"placeholder="请输入名称"autocomplete="off"class="layui-input"></div></div><div class="layui-form-item"><label class="layui-form-label">数量</label><div class="layui-input-block"><input type="number"name="numbers[]"required lay-verify="required"placeholder="请输入数量"autocomplete="off"class="layui-input"></div></div><div class="del"><input type="hidden"name="ids[]"value="0"/><button type="button"class="layui-btn layui-btn-primary layui-border-black layui-btn-sm del-btn">删除</button></div></div>'
                $(".row").append(colHtml)
            })
            $(document).on('click', '.del-btn', function() {
               $(this).parents(".col").remove()
            })
            $("#myForm").submit(function(){
                var formValues = $("#myForm").serializeArray();
                var param = {}
                $.each(formValues, function(i, field){
                    if(!param.hasOwnProperty(field.name)){
                        param[field.name]=[];
                    }
                    param[field.name].push(field.value)
                });
                $.post("face.php",param,function(res){
                    res = JSON.parse(res)
                    parent.layer.msg(res.msg);
                    if(res.code == 1){
                        var index = parent.layer.getFrameIndex(window.name); 
                        setTimeout(function(){
                            parent.layer.close(index);
                            //parent.location.reload()
                        },1000)
                    }
                })
                return false
            })
        });
    </script>  
</body>
</html>